//
//  ZRLoadingView.m
//  TestFile
//
//  Created by 黄涛 on 2018/6/6.
//  Copyright © 2018年 アポロジャパン. All rights reserved.
//

#import "ZRLoadingView.h"
#import "ZRRefreshHeader.h"
#import "ZRFishLayer.h"
@interface ZRLoadingView ()

@property (nonatomic , weak) UIActivityIndicatorView * indicatorView;

@end
@implementation ZRLoadingView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.style = circle;
        
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    if (self.progress <= 0) return;
    
    switch (self.style) {
        case circle:{[self animationOfCircle:rect];}break;
        case quadrate:{}break;
        case fish:{[self animationOfFish:rect];}break;
        default: break;
    }
  
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.indicatorView.center = CGPointMake(self.width * 0.5, self.height * 0.5);
}



#pragma mark - ----------------------- 其他方法 -----------------------

- (void)startAnimation{
    
    self.progress = 0;
    [self.indicatorView startAnimating];
}

- (void)stopAnimation{
    [self.indicatorView stopAnimating];
}

#pragma mark - 动画:  圆
- (void)animationOfCircle:(CGRect)rect{
 
    CGFloat line_w = 2;
    CGFloat x = rect.size.width * 0.5;
    CGFloat y = rect.size.height * 0.5;
    CGFloat r = (rect.size.width - line_w) * 0.5;
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctx, x, y, r, - M_PI_2, 2 * M_PI * self.progress , 0);
    CGContextSetStrokeColorWithColor(ctx, ZRMainColor.CGColor);
    CGContextSetLineWidth(ctx, line_w);
    CGContextStrokePath(ctx);

}

#pragma mark - 动画:  鱼
- (void)animationOfFish:(CGRect)rect{
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGPoint lines[] = {CGPointMake(15, rect.size.height /2),CGPointMake(rect.size.width - 15, rect.size.height / 2),CGPointMake(rect.size.width - 15 - 15, rect.size.height/2 - 15)};
    
    CGContextAddLines(ctx, lines, sizeof(lines) / sizeof(lines[0]));
    
     CGContextStrokePath(ctx);
    
    ZRFishLayer * shark = [ZRFishLayer layer];
    shark.bounds = CGRectMake(0, 0, 31, 31);
    shark.anchorPoint = CGPointMake(0, 0);
    shark.backgroundColor = [UIColor yellowColor].CGColor;
    [shark setNeedsDisplay];
    [self.layer addSublayer:shark];
    
    
    
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
    animation.fromValue = [NSValue valueWithCGPoint:self.layer.position];
    CGPoint toPoint = shark.position;
    toPoint.x += rect.size.width - 31 - 15;;
    animation.toValue = [NSValue valueWithCGPoint:toPoint];
    animation.duration = 1;
    [shark addAnimation:animation forKey:@"position.x"];
    
    
    
    
    
}




#pragma mark - ----------------------- 代理方法 -----------------------
#pragma mark - ----------------------- 活动监听 -----------------------
#pragma mark - ---------------------- set & get ----------------------

#pragma mark - 雏菊指示器
- (UIActivityIndicatorView *)indicatorView{
    if (!_indicatorView) {
        
        UIActivityIndicatorView * indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self addSubview:indicatorView];
        _indicatorView = indicatorView;
    }
    return _indicatorView;
}



#pragma mark - 动画风格
- (void)setStyle:(ZRLoadingStyle)style{
    _style = style;
    
}

#pragma mark - 拉取 进度
- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    [self setNeedsDisplay];
    
}

@end
